<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/token.jsp"%>
<%@ include file="/common/user.jsp"%>
<%
    String json = input.getDesc("data", Tools.encryptStr("{}"));
    Integer visitId = input.getInt("visit_id", -9999);
    Integer doctorUid = input.getInt("doctor_uid", 0);

    json = Tools.decryptStr(json); // 问卷二次解密
    Map jsonMap = JSONUtil.fromJson(json, new TypeReference<Map>(){});
    List quests = (List)jsonMap.get("quests");
    if (isEmpty(quests)) {
        msg = new Message(30062, "提交失败");
        Tools.encryptPrint(out, msg);
        return;
    }
    List<Integer> userIds = new ArrayList<Integer>();
    userIds.add(Tools.getInt(token.getUid()+"", 0));
    Map<Integer, User> users = usersByUids(userIds);
    User user = users.get(Tools.getInt(token.getUid()+"", 0));
    if (visitId <= 0) {
        Visit v = new Visit();
        v.setHospital(hospital);
        v.setPatientUid(token.getUid());
        v.setWorkId(token.getUid()+"");
        v.setCategory(3);
        v.setName(user.getName());
        v.setCheckTime(DateUtil.formatDate(new Date()));
        v.setSendTime(DateUtil.formatDate(new Date()));
        visitId = (Integer)DBTools.db("basedb").addItem(v);
    }
    if (visitId <= 0) {
        msg = new Message(30062, "提交失败");
        Tools.encryptPrint(out, msg);
        return;
    }
    int surveyId = Tools.getInt((jsonMap.get("id") + "").replace(".0", ""), 0);
    long time = System.currentTimeMillis();
    List<SurveyAnswer> surveyAnswers = new ArrayList<SurveyAnswer>();
    int sumScore = 0;
    for (int t = 0; quests != null && t < quests.size(); t++) {
        Map quest = (Map)quests.get(t);
        int quest_id = Tools.getInt((quest.get("quest_id") + "").replace(".0", ""), 0);
        int type = Tools.getInt((quest.get("type") + "").replace(".0", ""), 0);
        List options = (List)quest.get("options");
        for (int j = 0; options != null && j < options.size(); j++) {
            quest = (Map)options.get(j);
            SurveyAnswer surveyAnswer = new SurveyAnswer();
            surveyAnswer.setSurveyId(surveyId);
            surveyAnswer.setQuestId(quest_id);
            surveyAnswer.setDoctorUid(doctorUid);
            surveyAnswer.setContent(Tools.getDesc(quest.get("content") + "", ""));
            surveyAnswer.setHospital(hospital);
            surveyAnswer.setcTime(time);
            surveyAnswer.setUid(token.getUid());
            surveyAnswer.setScore(Tools.getInt(quest.get("score") + "", 0));
            sumScore += surveyAnswer.getScore();
            surveyAnswer.setVisitId(visitId);
            surveyAnswer.setType(type);
            surveyAnswers.add(surveyAnswer);
        }
    }
    List<Visit> visits = QBuilder.db("basedb").queryHql("from Visit where id = " + visitId);
    if (visits.size() > 0) {
        Visit v = visits.get(0);
        v.setScore(sumScore);
        v.setSurveyId(surveyId);
        v.setIsconfirmed(3);
        v.setReconfirmedTime(DateUtil.formatDate(new Date()));
        DBTools.db("basedb").modItem(v, v.getId());
    }
    DBTools.db("basedb").addItem(surveyAnswers);
    msg.put("data", visitId);
    Tools.encryptPrint(out, msg);
%>
